/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */


package com.smg.community.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javacommon.base.BaseRestSpringController;
import javacommon.util.HtmlToText;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.HtmlUtils;

import com.smg.community.model.SmgGroup;
import com.smg.community.model.SmgIndexRightbottomImage;
import com.smg.community.model.SmgIndexTopFiveimages;
import com.smg.community.model.SmgMember;
import com.smg.community.model.SmgTopic;
import com.smg.community.setting.Constant;
import com.smg.community.vo.query.SmgGroupQuery;
import com.smg.community.vo.query.SmgTopicQuery;
/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("")
public class IndexController extends BaseRestSpringController<SmgMember,java.lang.Integer>{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Log logger = LogFactory.getLog(IndexController.class);
	
	
	private void setIndexModel(ModelMap model,int cityNo){
		List<SmgIndexRightbottomImage> rightbottomImageList = smgIndexRightbottomImageManager.getOnLineRightBottomImageList(cityNo);
		model.addAttribute("rightbottomImageList",rightbottomImageList);
		List<SmgIndexTopFiveimages> topFiveImageLists = smgIndexTopFiveimagesManager.getOnLineTopFiveImageList(cityNo);
		model.addAttribute("topFiveImageLists",topFiveImageLists);
	}

	
	private void setCommenIndexModel(ModelMap model,int cityNo){
		//首页4个按24小时内回复数排序的热贴
				List<SmgTopic> fourHotTopicList = smgTopicManager.getFourHotTopicList(cityNo) ;
				if ( fourHotTopicList.size() == 0 ){
					SmgTopicQuery query = new SmgTopicQuery(); 
					query.setCityNo(cityNo);
					query.setStart(0);
					query.setLimit(4);
					query.setOrderBy("replycount desc");
					fourHotTopicList = smgTopicManager.getByQuery(query);
				}
				for(SmgTopic topic:fourHotTopicList) {
					topic.setTopicTitleAfterFilter( HtmlToText.getPlainText(topic.getTopicTitleAfterFilter()));
					topic.setTopicContAfterFilter(HtmlToText.getPlainText(topic.getTopicContAfterFilter()));
				}
				model.addAttribute("fourHotTopicList",fourHotTopicList);
				//首页7个按24小时内发帖数排序的热贴   群组的发帖+回帖不好实现  目前按发帖数
				List<SmgGroup> sevenHotGroupList = smgGroupManager.getSevenHotGroupList(cityNo) ;
				if ( sevenHotGroupList.size() == 0 ){
					SmgGroupQuery query = new SmgGroupQuery();
					query.setCityNo(cityNo);
					query.setOrderBy("membercount desc");
					query.setStart(0);
					query.setLimit(7);
					sevenHotGroupList = smgGroupManager.getByQuery(query);
				}
				model.addAttribute("sevenHotGroupList",sevenHotGroupList);
				//首页发帖+回帖最多的3个达人  这个数据从用户动态表里查找
				List<SmgMember> threeMemberList =  smgMemberManager.getThreeMemberByBehaviorCount();
				model.addAttribute("memberList",threeMemberList);
				List<SmgTopic> threeActivityList = smgTopicManager.getThreeActivityListOnline(cityNo);
				model.addAttribute("threeActivityList",threeActivityList);
				
	}
	/**
	 * @param model
	 * @return
	 * 3月31号开始做的首页 默认城市为上海
	 * 首页包括以下模块
	 * 1. 5张焦点图    推送
	 * 2. 活跃达人 是推送还是抓取?按什么规则抓取？
	 * 3. 小组热贴  推送还是抓取?按什么规则抓取?
	 * 4. 推荐小组 推送还是抓取？按什么规则抓取
	 * 
	 * 
	 */
	@RequestMapping("")
	
	public    String index_2013_3_31 (HttpServletRequest request,ModelMap model){
		System.out.println("in default index");
		return index_2013_3_31_othercity(request,model,21);
	}

	/**
	 * @param model
	 * @return
	 * 3月31号开始做的首页 默认城市为上海
	 * 首页包括以下模块
	 * 1. 5张焦点图    推送
	 * 2. 活跃达人 是推送还是抓取?按什么规则抓取？
	 * 3. 小组热贴  推送还是抓取?按什么规则抓取?
	 * 4. 推荐小组 推送还是抓取？按什么规则抓取
	 * 
	 * 
	 */
	@RequestMapping("/{cityNo}")
	
	public    String index_2013_3_31_othercity (HttpServletRequest request,ModelMap model,@PathVariable int cityNo){
		try {
			logger.debug("进入"+cityNo+"的首页");
			model.addAttribute("cityNo",cityNo);
			setCommunityCommonModel(request,model);
			setIndexModel(model,cityNo);
			setHeadTabIndex(model,Constant.INDEX_TAB);
			setCommenIndexModel(model,cityNo);
		}catch(Exception e){
			e.printStackTrace();
			toErrorPage(model, "出错了..", e);
		}
		return "index";
	}
	private void setPreviewIndexModel(ModelMap model,int cityNo){
		List<SmgIndexRightbottomImage> rightbottomImageList = smgIndexRightbottomImageManager.getPreviewRightBottomImageList(cityNo);
		model.addAttribute("rightbottomImageList",rightbottomImageList);
		List<SmgIndexTopFiveimages> topFiveImageLists = smgIndexTopFiveimagesManager.getPreviewLineTopFiveImageList(cityNo);
		model.addAttribute("topFiveImageLists",topFiveImageLists);
		//首页4个按24小时内回复数排序的热贴
		List<SmgTopic> fourHotTopicList = smgTopicManager.getFourHotTopicList(cityNo) ;
		if ( fourHotTopicList.size() == 0 ){
			SmgTopicQuery query = new SmgTopicQuery(); 
			query.setCityNo(cityNo);
			query.setStart(0);
			query.setLimit(4);
			query.setOrderBy("replycount desc");
			fourHotTopicList = smgTopicManager.getByQuery(query);
		}
		for(SmgTopic topic:fourHotTopicList) {
			topic.setTopicTitleAfterFilter( HtmlToText.getPlainText(topic.getTopicTitleAfterFilter()));
			topic.setTopicContAfterFilter(HtmlToText.getPlainText(topic.getTopicContAfterFilter()));
		}
		model.addAttribute("fourHotTopicList",fourHotTopicList);
		//首页7个按24小时内发帖数排序的热贴   群组的发帖+回帖不好实现  目前按发帖数
		List<SmgGroup> sevenHotGroupList = smgGroupManager.getSevenHotGroupList(cityNo) ;
		if ( sevenHotGroupList.size() == 0 ){
			SmgGroupQuery query = new SmgGroupQuery();
			query.setCityNo(cityNo);
			query.setOrderBy("membercount desc");
			query.setStart(0);
			query.setLimit(7);
			sevenHotGroupList = smgGroupManager.getByQuery(query);
		}
		model.addAttribute("sevenHotGroupList",sevenHotGroupList);
		//首页发帖+回帖最多的3个达人  这个数据从用户动态表里查找
		List<SmgMember> threeMemberList =  smgMemberManager.getThreeMemberByBehaviorCount();
		model.addAttribute("memberList",threeMemberList);
		List<SmgTopic> threeActivityList = smgTopicManager.getThreeActivityListOnline(cityNo);
		model.addAttribute("threeActivityList",threeActivityList);
	}
	/**
	 * 小组首页预览页
	 * @return
	 */
	@RequestMapping("/preview")
	public String index_2013_3_31_preview(HttpServletRequest request,ModelMap model){
			return index_2013_3_31_preview_othercity(request,model,21);
	}

	/**
	 * 小组首页预览页
	 * @return
	 */
	@RequestMapping("/preview/{cityNo}")
	public String index_2013_3_31_preview_othercity(HttpServletRequest request ,ModelMap model,@PathVariable int cityNo){
		setPreviewIndexModel(model,cityNo);
		setCommunityCommonModel(request,model);
		setHeadTabIndex(model,Constant.INDEX_TAB);
		return "index_preview";
	}
}

